package com.fucheng.transport;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.io.ObjectInputStream;
import java.util.List;

/**
 * 入站数据解码器：将字节流反序列化为 RpcRequest / RpcResponse 对象
 */
public class RpcDecoder extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {

        // 用 ObjectInputStream 反序列化字节为 Java 对象
        try(ObjectInputStream objectInputStream = new ObjectInputStream(new ByteBufInputStream(byteBuf))) {
            list.add(objectInputStream.readObject()); // 添加到 pipeline
        }
    }
}
